package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.SurfaceResize;
	import com.greensock.TweenLite;
	import com.manager.CarManager;
	import com.manager.Vision;
	import com.model.CarDataBase;
	import com.model.CarDataRemote;
	import com.vo.CarVo;
	import com.vo.CarportVo;
	import com.vo.KindVo;
	import com.vo.TypeVo;
	
	import feathers.controls.ComboBox;
	import feathers.controls.GroupedList;
	import feathers.controls.PickerList;
	import feathers.data.HierarchicalCollection;
	import feathers.data.ListCollection;
	import feathers.events.ItemEvent;
	
	import flash.utils.Dictionary;
	
	import mx.core.mx_internal;
	
	import starling.display.Button;
	import starling.display.DisplayObjectContainer;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.text.TextField;
	import starling.textures.Texture;
	/**
	 * 菜单栏下面的车型选择器
	 */
	public class ModelSelect extends SurfaceResize
	{
		private static var modelSelect:ModelSelect;
		public static function getInstance():ModelSelect{
			if(modelSelect == null)modelSelect = new ModelSelect();
			return modelSelect;
		}
		
		private var contain:Sprite;
		private var pickerKind:PickerList;
		private var pickerType:PickerList;
		private var pickerCar:PickerList;
		
		public function show():void{
			initFace();
			initData();
		}
		/**
		 *舞台尺寸改变 调整整体坐标位置
		 */		
		protected override function resize():void
		{
			TweenLite.to(contain,.2,{x:AwayEngine.senceWidth/2});
		}
		
		private function initFace():void
		{
			if(contain == null){
				contain = new Sprite();
				pickerKind = new PickerList();
				contain.addChild(pickerKind);
				pickerKind.addEventListener(ResizeEvent.RESIZE,addToStage);
			}
			contain.y = 35;
			Vision.addView(Vision.UI,contain);
		}
		
		private function addToStage(e:Event):void
		{
			contain.removeEventListener(ResizeEvent.RESIZE,addToStage);
			resize();
		}
		
		private function initData():void{
			initKind();
//			initType();
//			initCar();
//			begin();
//			showCar(0);
		}
		
//		private function initCar():void
//		{
//			updateCar();
//		}
//		
//		private function initType():void
//		{
//			updateType();
//		}
		
		private function initKind():void
		{
			pickerKind.listProperties.@itemRendererProperties.labelField = "text";
			pickerKind.width = 140;
			pickerKind.height = 20;
			pickerKind.labelField = "text";
			pickerKind.addEventListener(Event.CHANGE,onKindChange);
			pickerKind.dataProvider = getListCollection();
			pickerKind.selectedIndex = 0;
		}
		
		private function getListCollection():ListCollection{
			var list:Array = [];
			var kindList:Vector.<KindVo> = CarDataRemote.kindList;
			for (var i:int = 0; i < kindList.length; i++) 
			{
				list.push({kindID:kindList[i].kindID,text:kindList[i].cName});
			}
			return new ListCollection(list);
		}
		
//		private function begin():void{
//			
//		}
		private function onKindChange(e:Event = null):void
		{
			updateType();
//			switch(pickerKind.selectedIndex){
//					case 0:
//						var list:Array = CarDataBase.carNameList;
//						break;
//					case 1:
//						list = CarDataBase.chevroletCamaroList;
//						break;
//					case 2:
//						list = CarDataBase.ferrariCaliforniaList;
//						break;
//					case 3:
//						list = CarDataBase.mercedesMCLarenSLR500List;
//						break;
//					case 4:
//						list = CarDataBase.mitsubishiLancerXList;
//						break;
//					case 5:
//						list = CarDataBase.nissan350zList;
//						break;
//					case 6:
//						list = CarDataBase.astonMartinVintageList;
//						break;
//			}
//			CarManager.showCar(list[0].model);
//			trace((list.selectedItem).name)
//			trace( "selectedGroupIndex:"+ list.selectedGroupIndex,CarDataBase.carNameList[list.selectedGroupIndex].name);
		}
//		private function showCar(index:int):void{
//			var obj:Object = pickerKind.dataProvider.getItemAt(index);
//			CarManager.showCar(CarDataBase.carNameList[index].model);
//		}
		
		private function updateCar():void{
			if(pickerType.selectedItem == null){
				trace('无此品牌数据');
				return;
			}
			var typeID:int = pickerType.selectedItem.typeID;
			var kindID:int = pickerKind.selectedItem.kindID;
			TypeFace.getInstance().showType(CarDataRemote.getTypeVo(typeID,kindID));
			if(pickerCar != null){
				pickerCar.parent.removeChild(pickerCar);
				pickerCar.removeEventListener(Event.CHANGE,onCarChange);
			}
			pickerCar = getPickerCar(kindID,typeID);
			contain.addChild(pickerCar);
			pickerCar.addEventListener(Event.CHANGE,onCarChange);
			onCarChange();
//			_carVo = cList != null && cList.length > 0 ? cList[0]:null;
		}
		private function getPickerCar(kindID:int,typeID:int):PickerList{
			var cList:Vector.<CarVo> = CarDataRemote.getCarListByType(typeID);
			var ct:PickerList = pickerDic[kindID + '_' + typeID];
			//根据条目数量动态改变
			if(ct == null){
				ct = pickerDic[kindID + '_' + typeID] = new PickerList();
				ct.listProperties.@itemRendererProperties.labelField = "text";
				ct.width = 140;
				ct.height = 20;
				ct.labelField = "text";
				ct.x = 310;
				var listCollection:ListCollection = new ListCollection(/*[{ text: "选择品牌"}]*/);
				for (var i:int = 0; i < cList.length; i++){
					listCollection.push({carID:cList[i].carID,text:cList[i].cName});
				}
				ct.dataProvider = listCollection;
			}
			ct.selectedIndex = 0;
			return ct;
		}
		
		private function onCarChange(e:Event = null):void
		{
			if(e == null)ct = pickerCar;
			else var ct:PickerList = e.currentTarget as PickerList;
			if(ct.selectedItem != null){
				_carVo = CarDataRemote.getCarVo(ct.selectedItem.carID);
				MenuFace.getInstance().refreshPart();
				if(_carVo != null)
				CarManager.showCar(_carVo);
			}
		}
		
		private function updateType():void{
			var kindID:int = pickerKind.selectedItem.kindID;
			if(pickerType != null){
				pickerType.parent.removeChild(pickerType);
				pickerType.removeEventListener(Event.CHANGE,onTypeChange);
			}
			pickerType = getPickerType(kindID);
			contain.addChild(pickerType);
			pickerType.addEventListener(Event.CHANGE,onTypeChange);
			onTypeChange();
		}
		
		private static var pickerDic:Dictionary = new Dictionary(true);
		private function getPickerType(kindID:int):PickerList{
			var typeList:Vector.<TypeVo> = CarDataRemote.getTypeList(kindID)
			var ct:PickerList = pickerDic[kindID];
			//根据条目数量动态改变
			if(ct == null){
				ct = pickerDic[kindID] = new PickerList();
				ct.listProperties.@itemRendererProperties.labelField = "text";
				ct.width = 140;
				ct.height = 20;
				ct.labelField = "text";
				ct.x = 155;
				var listCollection:ListCollection = new ListCollection(/*[{ text: "选择车系"}]*/);
				for (var i:int = 0; i < typeList.length; i++){
					listCollection.push({typeID:typeList[i].typeID,text:typeList[i].cName});
				}
				ct.dataProvider = listCollection;
				ct.selectedIndex = 0;
			}
			return ct;
		}
		
		private function onTypeChange(e:Event = null):void
		{	
			updateCar();
		}
		
		/*public function set vision(value:Boolean):void
		{
			contain.visible = value;
		}*/
		
		private var _carVo:CarVo;
		public function get carVo():CarVo{
			return _carVo;
		}
		
	}
}